// pages/ucenter/addAddress/addAddress.js
const api = require('../../../config/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {
      address: '', //详细地址
      city_id: 0, //城市ID
      district_id: '', //地区ID
      is_default: false, //是否设为默认
      mobile: '', //手机号码
      name: '', //姓名
      province_id: '', //省份ID
      id: 0, //修改收货地址ID（添加收货地址时传入0）
    },
    region: [
      '省份', '城市', '区县'
    ]
  },
  // 以下是自定义的方法

  // 姓名
  inputName(e) {
    console.log(e);
    let address = this.data.address;
    address.name = e.detail.value
    this.setData({
      address
    })
  },
  // 电话
  inputPhone(e) {
    let address = this.data.address;
    address.mobile = e.detail.value
    this.setData({
      address
    })
  },
  // 省区市
  bindRegionChange(e) {
    console.log(e);
    let address = this.data.address;
    // address.province_id=e.detail.code[0];
    address.province_id = 3;
    // address.city_id=e.detail.code[1];
    address.city_id = 38;
    // address.district_id=e.detail.code[2]
    address.district_id = 422
    this.setData({
      region: e.detail.value,
      address
    })
    // console.log(this.data.address);

  },
  // 地址详情
  inputDetail(e) {
    let address = this.data.address;
    address.address = e.detail.value;
    this.setData({
      address
    })
    // console.log(this.data.address);
  },
  // 是否为默认地址
  isDefault() {
    let address = this.data.address;
    address.is_default = !address.is_default;
    console.log(address.is_default);

    this.setData({
      address
    })

  },

  // 取消
  bindCancel() {
    wx.navigateBack({
      delta: 1
    })
  },
  // 保存
  bindSave() {
    wx.request({
      url: api.saveAddressUrl,
      method: 'POST',
      data: {
        address: this.data.address.address, //详细地址
        city_id: this.data.address.city_id, //城市ID
        district_id: this.data.address.district_id, //地区ID
        is_default: this.data.address.is_default, //是否设为默认
        mobile: this.data.address.mobile, //手机号码
        name: this.data.address.name, //姓名
        province_id: this.data.address.province_id, //省份ID
        id: this.data.address.id //修改收货地址ID（添加收货地址传入0）
      },
      header: {
        'content-type': 'application/json',
        'X-Nideshop-Token': wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res);
        wx.navigateBack({
         delta:1
        })

      },
      fail: () => {
        console.log('请求失败');

      }
    })
  },
  // 修改地址时候获取地址内容
  getEditAddress() {
    wx.request({
      url: api.getEditAddressUrl,
      data: {
        id: this.data.address.id
      },
      header: {
        'content-type': 'application/json',
        'X-Nideshop-Token': wx.getStorageSync('token')
      },
      success: (res) => {
        // console.log(res);
        let data = res.data.data;
        console.log(data);

        let address = this.data.address;
        let region = this.data.region;
        address.name = data.name;
        address.mobile = data.mobile;
        address.address=data.address
        region[0] = data.province_name;
        region[1] = data.city_name;
        region[2] = data.district_name;
        console.log(address);
        
        this.setData({
          address,
          region
        })
      },
      fail: () => {
        console.log('获取失败');

      }
    })
  },




  //  * 生命周期函数--监听页面加载
  //  */
  onLoad: function (options) {
    if (options.id) {
      // console.log(options.id);
      let address = this.data.address;
      address.id = options.id;
      this.setData({
        address
      })
      this.getEditAddress();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})